package com.weixing.mall.provider.controller.common;


import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.core.support.auth.AgentAuth;
import com.weixing.mall.core.support.auth.AgentInfo;
import com.weixing.mall.core.support.auth.MemAuth;
import com.weixing.mall.core.support.auth.MemInfo;
import com.weixing.mall.provider.model.domain.AgentDeliveryAddress;
import com.weixing.mall.provider.model.dto.DeliveryDto;
import com.weixing.mall.provider.model.vo.AgentDeliveryAddressVo;
import com.weixing.mall.provider.service.IAgentDeliveryAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 *
 * 代理模块-收货地址表 前端控制器
 *
 *
 * @author Small
 * @since 2020-05-07
 */
@RestController
@RequestMapping("/delivery/common")
@Api(value = "代理商", tags = "前端:代理商地址管理通用接口API")
public class DeliveryCommonController extends BaseController {
    @Autowired
    private IAgentDeliveryAddressService agentDeliveryAddressService;

    @GetMapping("app/{mobile}")
    @ApiOperation(value = "根据手机号查找代理商地址--APP使用")
    public Result selectByMobileForApp(@MemInfo MemAuth auth, @PathVariable String mobile) {
        List<AgentDeliveryAddressVo> vos = agentDeliveryAddressService.selectByMobile(mobile, auth.getAbid());
        return ResultUtil.success(vos);
    }

    @GetMapping("app/add")
    @ApiOperation(value = "新增收货地址")
    public Result add(@MemInfo MemAuth auth, DeliveryDto deliveryDto) {
        AgentDeliveryAddress address = new AgentDeliveryAddress();
            BeanUtils.copyProperties(deliveryDto, address);
            if (null == deliveryDto.getId()) {
                address.setAgentId(auth.getAbid());
                agentDeliveryAddressService.save(address);
            } else {
                agentDeliveryAddressService.updateById(address);
        }
        return ResultUtil.success();
    }

    @GetMapping("sys/{mobile}")
    @ApiOperation(value = "根据手机号查找代理商地址----后台使用")
    public Result selectByMobileForAdmin(@AgentInfo AgentAuth auth, @PathVariable String mobile) {
        List<AgentDeliveryAddressVo> vos = agentDeliveryAddressService.selectByMobile(mobile, auth.getAid());
        return ResultUtil.success(vos);
    }

}
